import axios from 'axios'
import md5 from 'md5'
/**
 * 环境配置 APP_ENVIRONMENT
 * 开发 ------ developWechat | developAlilife
 * 测试 ------ testWechat | testAlilife
 * 正式 ------ productionWechat | productionAlilife
*/
export const APP_ENVIRONMENT = 'productionWechat'
const CONFIG = {
  developWechat: {
    base: '/developWechat/',
    CHILD_URL: '',
    TRANSFER_URL: 'http://localhost:9090/transfer'
  },
  testWechat: {
    base: '/testWechat/',
    CHILD_URL: '',
    TRANSFER_URL: 'http://localhost:9090/transfer'
  },
  productionWechat: {
    base: '/report-wechat/',
    // base: '/wechatclient/api/',
    CHILD_URL: '/wechatclient',
    TRANSFER_URL: 'http://wxtest01.atag.bsoft.com.cn/wechatclient/#/transfer'
  },
  developAlilife: {
    base: '/developAlilife/',
    CHILD_URL: '',
    TRANSFER_URL: 'http://localhost:9090/transfer'
  },
  testAlilife: {
    base: '/testAlilife/',
    CHILD_URL: '',
    TRANSFER_URL: 'http://localhost:9090/transfer'
  },
  productionAlilife: {
    base: '/alilife/api/',
    CHILD_URL: '/alilife',
    TRANSFER_URL: 'http://wxtest01.atag.bsoft.com.cn/wechatclient/#/transfer'
  }
}
export const DOCTOR_IMG_URL = 'http://219.146.149.178:53505/doctorphoto/'
export const APP_PLATFORM = APP_ENVIRONMENT.indexOf('Wechat') > -1 ? 1 : 2
export var base = CONFIG[APP_ENVIRONMENT].base
export const TRANSFER_URL = CONFIG[APP_ENVIRONMENT].TRANSFER_URL
export const CHILD_URL = CONFIG[APP_ENVIRONMENT].CHILD_URL

/**
 * 通用请求接口
 * @param {String} url 请求路径
 * @param {Object} params 请求参数
 * @param {Boolean} needAuth 是否需要登录 默认false
 */
export const commonAjax = (url, params = {}, needAuth = false) => {
  var instance = axios.create({
    headers: {
      'X-Access-Token': sessionStorage.getItem('accessToken'),
      'Content-Type': 'application/x-www-form-urlencoded'
      // 'X-Signature': getSignature(params)
    }
  })
  if (needAuth) {
    let temp = APP_PLATFORM === 1 ? _transformRequest(params) : _transformRequest(_getSignature(params))
    return instance.post(`${base}auth/${url}`, temp)
  } else {
    return instance.post(`${base}${url}`, _transformRequest(params))
  }
}

// 参数签名验证
function _getSignature (params) {
  const t = Date.now()
  let newParams = Object.assign(params, {
    timestamp: t
  })
  let keys = Object.keys(newParams).sort()
  let temp = ''
  keys.forEach(v => {
    temp += newParams[v]
  })
  return _transformUrl(Object.assign(newParams, {
    sign: md5(temp),
    timestamp: t
  }))
}
function _transformUrl (obj) {
  for (let i in obj) {
    obj[i] = encodeURIComponent(obj[i])
  }
  return obj
}
function _transformRequest (params) {
  let s = ''
  for (let i in params) {
    s += encodeURIComponent(i) + '=' + encodeURIComponent(params[i]) + '&'
  }
  return s
}
/**
 * 接口地址
 */
export const RESTFUL_API = {
  // 普通发送验证码
  sendSmsCode: 'user/sendSmsCode',
  // 绑定新手机号发送验证码
  bindcaptcha: 'util/bindcaptcha',
  // 校验验证码
  verifyCode: 'user/verifyCode',
  // 查询用户是否存在
  findUser: 'user/findUser',
  // 绑定新用户
  bind: 'user/bind',
  // 完善个人信息
  modifyAccount: 'user/modifyAccount',
  // 解绑
  unbind: 'user/unbind',
  // 添加就诊人
  addMember: 'user/addMember',
  // 修改就诊人
  updateMember: 'user/updateMember',
  // 删除就诊人
  removeMember: 'user/removeMember',
  // 就诊人列表
  listFamily: 'user/listFamily',
  // 就诊人详情
  detail: 'user/detai',
  // 添加卡认证
  addCard: 'user/addCard',
  // 删除认证
  removeCard: 'user/removeCard',
  removeCardByPatientCode: 'user/removeCardByPatientCode',
  addCards: 'user/addCards',
  // 获取我的信息
  getSelf: 'user/getSelf',
  // 手机号与身份证号是否匹配
  mathMobileAndIdCard: 'user/mathMobileAndIdCard',
  // 修改手机号
  updateMobile: 'user/updateMobile',
  // 医院介绍
  getHosptialInfo: 'news/getHosptialInfo',
  // 新闻公告
  Announcement: 'news/listAnnouncement',
  // 健康宣教
  list: 'news/list',
  // 患者信息是否匹配 needAuth
  patientIsMatch: 'register/patientIsMatch',
  // 获取科室列表 needAuth
  listDepartment: 'appointment/listDepartment',
  // 获取医生列表 needAuth
  listDoctor: 'appointment/listDoctor',
  // 搜索科室、医生 needAuth
  queryDeptAndDoctor: 'appointment/queryDeptAndDoctor',
  // 医生主页 needAuth
  getDoctorHomePage: 'appointment/getDoctorHomePage',
  // 获取号源 needAuth
  listNumber: 'appointment/listNumber',
  // 提交预约
  confirmAppointment: 'appointment/confirmAppointment',
  // 待就诊
  listAppointmentWait: 'appointment/listAppointmentWait',
  // 预约支付
  appointmentPay: 'appointment/appointmentPay',
  // 取消预约
  appointmentPayCancel: 'appointment/appointmentPayCancel',
  // 预约列表
  listAppointment: 'appointment/listAppointment',
  // 检查报告
  listCheckReport: 'checkreport/listCheckReport',
  // 报告查看
  listInspectionReport: 'inspectionreport/listInspectionReport',
  // 排队叫号
  listQueues: 'queues/listQueues',
  // 门诊费用
  listOutpatientExpenses: 'outpatient/listOutpatientExpenses',
  // 住院 一日清单
  listBill: 'onedaybill/listBill',
  // 历史住院记录
  listHospitalizationRecord: 'hospitalization/listHospitalizationRecord',
  // 费用明细
  listHospitalizationPayment: 'hospitalization/listHospitalizationPayment',
  listHospitalizationPaymentDate: 'hospitalization/listHospitalizationPaymentDate',
  // 住院预交金充值
  hospitalizationDeposit: 'hospitalization/hospitalizationDeposit',
  // 预缴记录
  listPaymentHistory: 'hospitalization/listPaymentHistory',
  // 科室介绍
  listLeafDepartment: 'home/listLeafDepartment',
  // 医生介绍
  listDoctorByDept: 'home/listDoctorByDept',
  // 搜索科室和医生
  queryDeptAndDoctor2: 'home/queryDeptAndDoctor',
  // 门诊候诊信息查询
  mzQueue: 'queues/listQueues',
  // 门诊费用查询
  mzFee: 'outpatient/listOutpatientExpenses',
  // 查询检验报告
  jyReport: 'inspectionreport/listInspectionReport',
  // 查询检查报告
  jcReport: 'checkreport/listCheckReport',
  // 治疗查询
  queryTreatment: 'price/queryTreatment',
  // 药品查询
  queryDrugs: 'price/queryDrugs',
  // 账户查询
  queryAccount: 'hisaccount/queryAccount',
  // 交易记录
  queryRecord: 'trade/queryRecord',
  // 身份验证，用手机号获取验证码
  confirmIdByPhone: 'user/sendSmsCode4Auth',
  // 满意度调查-获取门诊记录
  listMedicalRecords: 'satisfaction/listMedicalRecords',
  // 满意度调查-获取住院记录
  getHospitalizationRecords: 'satisfaction/getHospitalizationRecords',
   // 住院预缴金-查询预缴金余额
  getBalance: 'hospitalization/getBalance',
   // 微信支付接口
  wxPay: 'wxPay/prepay',
  // 诊间支付-待支付列表
  listPayment: 'diagnosispayment/listPayment',
  // 诊间支付-已支付列表
  listPaymented: 'diagnosispayment/listPaymented',
  // 诊间支付-待支付详情
  paymentDetails: 'diagnosispayment/paymentDetails',
  // 诊间支付-预结算
  paymentBudget: 'diagnosispayment/paymentBudget',
  // 诊间支付-查询药品清单
  listDrug: 'diagnosispayment/listDrug',
  // 体检预约-获取体检套餐列表
  getSet: 'physical/getSet',
  // 问卷详情
  getByType: 'question/getByType',
  // 保存问卷
  createResult: 'question/createResult',
  // getHisPhone
  getHisPhone: 'register/getHisPhone',
  // 签到取号列表
  listTakeNumber: 'takenumber/listNumber',
  // 签到
  signIn: 'takenumber/signIn',
  // 获取挂号单
  getRegistrationForm: 'takenumber/getRegistrationForm',
  // 微信支付接口
  getPayInfo: 'agWXPay/getPayInfo',
  queryPayResult: 'agWXPay/queryPayResult',
  // 支付宝支付接口
  getPayInfoAlipay: 'agLifePay/getPayInfo',
  payProcessTradeAlipay: 'agLifePay/payProcessTrade',
  notify: 'agWXPay/notify',
  payProcessTrade: 'agWXPay/payProcessTrade',
  unpaidAmount: 'hisaccount/unpaidAmount',
  createOrderNo: 'payment/createOrderNo'
}

